package com.hncboy.chatgpt.front.controller;



import com.hncboy.chatgpt.front.domain.ChatDTO;
import com.hncboy.chatgpt.front.domain.Message;
import com.hncboy.chatgpt.front.domain.ProStateManagement;
import com.hncboy.chatgpt.front.service.ChatCacheService;
import com.hncboy.chatgpt.front.service.IProProgressService;
import com.hncboy.chatgpt.front.util.AjaxResultBox;
import com.hncboy.chatgpt.front.util.ChatParams;
import com.hncboy.chatgpt.front.util.ChatResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * chatGpt Ai Controller
 *
 * @author
 * @date 2023-04-24
 */
@Controller
public class ProProgressController {


    @Autowired
    private IProProgressService iProProgressService;

    @Autowired
    private ChatCacheService cacheService;

    /**
     * 单次人工对话（replyContent['data']）
     * {
     *     "chatId": "1",
     *     "content": "你能做什么"
     * }
     * @param proStateManagement
     * @return
     */
    @CrossOrigin(origins = "*")
    @PostMapping("pack/progress/chat")
    @ResponseBody
    public AjaxResultBox completions(@RequestBody ProStateManagement proStateManagement)
    {
        return iProProgressService.sseChat(proStateManagement);
    }

    /**
     * 能记9次人工对话（replyContent['msg']）
     * {
     *     "chatId": "1",
     *     "content": "你能做什么"
     * }
     * @param dto
     * @return
     */
    @PostMapping("pack/progress/chatS")
    @ApiOperation(value = "聊天",notes = "")
    @ResponseBody
    public AjaxResultBox doChat(@RequestBody ChatDTO dto){
        ChatParams params = new ChatParams();
        List<Message> messages = cacheService.history(dto.getChatId(), dto.getWithContext());
        // 构建用户信息
        Message userMsg = iProProgressService.buildUserMessage(dto.getContent());
        messages.add(userMsg);
        params.setMessages(messages);
        // 写入缓存服务
        cacheService.write(dto.getChatId(), userMsg);
        // 执行
        ChatResult result = iProProgressService.doChat(params, dto.getChatId());
        String s = cacheService.simpleResult(result);
        return AjaxResultBox.success(s);
    }

}
